package com.wuyi.tea.controller.common;

import com.wuyi.tea.domain.dto.CommentDTO;
import com.wuyi.tea.domain.dto.ProductDTO;
import com.wuyi.tea.domain.pojo.Product;
import com.wuyi.tea.domain.vo.CommentVO;
import com.wuyi.tea.domain.vo.ProductDetailVO;
import com.wuyi.tea.domain.vo.ProductVO;
import com.wuyi.tea.domain.vo.R;
import com.wuyi.tea.service.CommentService;
import com.wuyi.tea.service.ProductService;
import com.wuyi.tea.util.LocalStorageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Api(tags = "商品模块")
@RestController(value = "CommonProductController")
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    @Autowired
    private CommentService commentService;


    @ApiOperation("获取商品列表")
    @GetMapping("/list")
    public R getProductList(@RequestParam(defaultValue = "1") Long categoryId){
        log.info("id为{}的用户获取了商品列表", LocalStorageUtil.get());
        List<Product> productList=productService.selectById(categoryId);
        return R.success(productList);
    }

    @ApiOperation("获取商品推荐")
    @GetMapping("/recommend")
    public R getProductRecommend(@RequestParam Long categoryId){

        log.info("id为{}的用户获取分类为{}的商品推荐",LocalStorageUtil.get(),categoryId);
        List<ProductVO>productVOS = productService.getProductRecommend(categoryId);
        return R.success(productVOS);
    }


    @ApiOperation("获取商品详情")
    @GetMapping("/detail")
    public R getProductDetail(@RequestParam Long productId){

        log.info("id为{}的用户获取了商品详情,商品id:{}",LocalStorageUtil.get(), productId);
        ProductDTO product =productService.listByProductId(productId);
        return R.success(product);
    }

    //  对商品的评论功能
    @ApiOperation("获取商品评论")
    @GetMapping("/comment/list")
    public R getProductComment(@RequestParam Long productId){

        log.info("id为{}的用户获取了id为{}的商品的所有评论",LocalStorageUtil.get(),productId);
        List<CommentVO>commentVOS = commentService.getProductCommentList(productId);
        return R.success(commentVOS);
    }

    @ApiOperation("发布评论")
    @PostMapping("/comment")
    public R releaseComment(@RequestParam Long productId,
                            @RequestBody CommentDTO commentDTO
                ){

        log.info("id为{}的用户正在发布评论:\n{}",LocalStorageUtil.get(),commentDTO);
        commentService.releaseComment(productId,commentDTO);
        return R.success("评论成功",null);
    }


    @ApiOperation("删除评论")
    @DeleteMapping("/comment")
    public R deleteComment(@RequestParam Long commentId){

        log.info("id为{}的用户正在删除评论：{}",LocalStorageUtil.get(),commentId);
        commentService.deleteProductComment(commentId);
        return R.success();
    }

    @ApiOperation("点赞评论")
    @PostMapping("/comment/like")
    public R likeComment(@RequestParam Long commentId){

        log.info("id为{}的用户给id为{}的评论进行了点赞",LocalStorageUtil.get(),commentId);
        commentService.likeComment(commentId);
        return R.success();
    }

}
